<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>TITLE</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input type="hidden" th:value="${id} " id="id" name="id" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">关键字</label>
            <div class="layui-input-inline">
                <textarea id="problem" name="problem" style="width: 500px;height: 100px;" placeholder="请输入问题" class="layui-input"></textarea>

            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">答案</label>
            <div class="layui-input-inline">
                <textarea id="answer" name="answer" style="width: 500px;height: 100px;" placeholder="请输入答案" class="layui-input"></textarea>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否完全匹配</label>
        <div class="layui-input-inline">
            <input type="checkbox" id="isMatch" name="isMatch" lay-skin="switch" lay-filter="switch" lay-text="是|否"
                   checked>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit">保存</button>
            <button class="layui-btn layui-btn-primary" onclick="cenCel()">取消</button>
        </div>
    </div>
</div>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var tree = layui.tree;
    var curIfr = parent.layer.getFrameIndex(window.name);
    window.onload = function () {
        var id = $("#id").val()
        if (!CoreUtil.isEmpty(id)) {
            CoreUtil.sendGet('/dialogue/' + id, null, function (res) {
                if (res.data != null) {
                    $("#id").val(res.data.id);
                    $("#problem").val(res.data.problem);
                    $("#answer").val(res.data.answer);
                    $("#deleted").val(res.data.deleted);
                    if (res.data.isMatch == 1) {
                        $("#isMatch").attr('checked', 'checked');
                        $("#isMatch").attr('type', 'hidden').val(1);
                    } else {
                        $("#isMatch").attr('type', 'hidden').removeAttr("checked").val(0);
                        var x = document.getElementsByClassName("layui-unselect layui-form-switch layui-form-onswitch");
                        x[0].setAttribute("class", "layui-unselect layui-form-switch");
                        var d = document.getElementsByTagName('em')[0];
                        d.firstChild.nodeValue = '否';
                    }
                }
            })
        } else {
            $("#isMatch").attr('checked', 'checked');
            $("#isMatch").attr('type', 'hidden').val(1);
        }
    }
    //监听提交
    form.on('submit(submit)', function (data) {
        if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
            data.field.deleted = 1;
            CoreUtil.sendPost("/dialogue/add", data.field, function (res) {
                if (res.code == 0) {
                    //跳出提示
                    parent.layer.msg(res.msg);
                    //刷新表格
                    setTimeout(function () {
                        parent.location.reload();
                    }, 1000)
                } else {
                    layer.msg(res.msg);
                }
            });
        } else {
            CoreUtil.sendPut("/dialogue/update", data.field, function (res) {
                if (res.code == 0) {
                    //跳出提示
                    parent.layer.msg(res.msg);
                    //刷新表格
                    setTimeout(function () {
                        parent.location.reload();
                    }, 1000)
                } else {
                    layer.msg(res.msg);
                }
            });

        }
        return false;
    });

    form.on('switch(switch)', function () {
        $("#isMatch").attr('type', 'hidden').val(this.checked ? 1 : 0);
    });

    function cenCel() {
        parent.layer.close(curIfr);
    }


</script>
</body>
</html>